package fr.enseirb.mycub.activity;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.preference.Preference;

import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import fr.enseirb.mycub.PrefManager;
import fr.enseirb.mycub.R;
import fr.enseirb.mycub.service.ILocationServiceListener;
import fr.enseirb.mycub.service.LocationService;
import fr.enseirb.mycub.service.LocationService.LocationBinder;
import fr.enseirb.mycub.utils.ActivityHelper;
import fr.enseirb.mycub.utils.DetachableResultReceiver;

abstract public class AbstractPreferencesActivity extends SherlockPreferenceActivity
		implements OnSharedPreferenceChangeListener, OnClickListener,
		ILocationServiceListener, DetachableResultReceiver.Receiver
		 {

	public static final String NETWORK_PREFERENCE = "network";
	public static final String REPORT_BUG_PREFERENCE = "report_bug";
	public static final String FEEDBACK_PREFERENCE = "feedback";
	public static final String FAVORITE_FILTER = "favorite_filter";
	public static final String BIKES_FILTER = "bikes_filter";
	public static final String SLOTS_FILTER = "slots_filter";
	public static final String ENABLE_DISTANCE_FILTER = "enable_distance_filter";
	public static final String DISTANCE_FILTER = "distance_filter";
	public static final String LOCATION_PREFERENCE = "use_location";
	public static final String CENTER_PREFERENCE = "center_on_location";
	public static final String UPDATE_SERVER_URL = "server_url";
	public static final String NETWORK_LATITUDE = "network_latitude";
	public static final String NETWORK_LONGITUDE = "network_longitude";
	public static final String NETWORK_NAME = "network_name";
	public static final String NETWORK_CITY = "network_city";
	public static final String SPECIAL_STATION = "special_station";
	public static final String LAST_UPDATE_BIKE = "last_update_bike";
	public static final String LAST_UPDATE_BUS = "last_update_bus";
	public static final String LAST_UPDATE_TRAM = "last_update_tram";
	public static final String STATIONS_VERSION = "stations_version";

	public static final int NO_NETWORK = 0;

	protected Preference mNetworkPreference;
	protected Preference mReportBugPreference;
	protected Preference mFeedbackPreference;
	protected Dialog mConfirmDialog;
	private Location mLastLocation = null;
	protected ActivityHelper mActivityHelper = null;
	protected DetachableResultReceiver mReceiver = null;
	private boolean mBound = false;
	private LocationService mService = null;

	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {
			// We've bound to LocalService, cast the IBinder and get
			// LocalService instance
			LocationBinder binder = (LocationBinder) service;
			mService = binder.getService();
			mBound = true;
			mService.addListener(AbstractPreferencesActivity.this);
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mBound = false;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preference_screen);
		mReceiver = DetachableResultReceiver.getInstance(new Handler());
		
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		//mActivityHelper.onPostCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();
		mReceiver.setReceiver(this);
	}

	@Override
	protected void onStart() {
		if (PrefManager.mgr.getBoolean(
				AbstractPreferencesActivity.LOCATION_PREFERENCE, true)) {
			//Intent intent = new Intent(this, LocationService.class);
			//bindService(intent, mConnection, 0);
		}
		super.onStart();
	}

	@Override
	protected void onPause() {
		mReceiver.clearReceiver();
		super.onPause();
	}

	@Override
	protected void onStop() {
		if (mBound) {
			unbindService(mConnection);
			mService.removeListener(AbstractPreferencesActivity.this);
			mBound = false;
		}
		super.onStop();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportActionBar().setTitle("Preferences");
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		default:
			//ActivityHelper.startSync(this);
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onClick(DialogInterface arg0, int arg1) {

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		default:
			Dialog dialog = getActivityHelper().onCreateDialog(id);
			if (dialog != null)
				return dialog;
		}
		return super.onCreateDialog(id);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		getActivityHelper().onPrepareDialog(id, dialog);
		super.onPrepareDialog(id, dialog);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals(AbstractPreferencesActivity.LOCATION_PREFERENCE)) {
			// Log.i("OpenBike", "Location changed");
			if (sharedPreferences.getBoolean(
					AbstractPreferencesActivity.LOCATION_PREFERENCE, true)) {
				Intent intent = new Intent(this, LocationService.class);
				bindService(intent, mConnection, 0);
			} else if (mBound) {
				mService.removeListener(AbstractPreferencesActivity.this);
				mBound = false;
				unbindService(mConnection);
			}
		}
	}

	@Override
	public void onLocationChanged(Location location, boolean unused) {
		if (location == mLastLocation) {
			return;
		}
		mLastLocation = location;
	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		// We are not interested in anything
	}

	/**
	 * Returns the {@link ActivityHelper} object associated with this activity.
	 */
	public ActivityHelper getActivityHelper() {
		return mActivityHelper;
	}

	@Override
	public void onLocationProvidersChanged(int id) {
		if (!isFinishing()) {
			showDialog(id);
		}
	}

	@Override
	public void onStationsUpdated() {
	}

}
